#include "GameManager.h"

#include <Network/Server.h>
#include <Network/ServerUser.h>

#include <Game/World/Systems/ZoneSystem.h>
#include <Game/World/Systems/RoomSystem.h>

#include <Game/Entity.h>

//WORLD Components
#include <Game/World/Components/Zone.h>
#include <Game/World/Components/Room.h>

using namespace Game;

GameManager::GameManager(Network::Server &server)
	: server(server), zoneSystem(NULL)
{
	//Just an early test event. Of course users won't be able to start the actual game world every time they log in :P
	user_game_events.func_event("Game-RequestStart").set(this, &Game::GameManager::onEventGameRequestStart);

	zoneSystem = new World::Systems::ZoneSystem(*this);
}

GameManager::~GameManager()
{
	if(zoneSystem)
		delete zoneSystem;
}

void GameManager::update(const unsigned int &pulse_ms)
{
	//Pulse game world every time server process times out (10ms default)
	float dt = (float)pulse_ms / 1000.0f;
	zoneSystem->update(dt);
}

void GameManager::onEventGameRequestStart(const CL_NetGameEvent &e, Network::ServerUser *user)
{
	cl_log_event("Events", "Client requested game start");

	if(server.isGameRunning() == false)
	{
		server.startGame();

		//Create a test world
		Entity &zone_test = zoneSystem->createZone();
		
		server.getNetGame().send_event(CL_NetGameEvent("Game-Start"));
	}
}
